package com.example.ui.contact;



import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.NetContact;
import com.example.service.MainService;
import com.example.ui.contact.chat.L;
import com.example.ui.contact.chat.SharePreferenceUtil;
import com.example.ui.leftmenu.Item08View;
import com.example.util.Util;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 加学友主界面
 * @author Administrator
 *
 */
public class AddFriendActivity extends Activity{
	private Context context;
	private ListView add_listview;
	private AddListViewAdapter adapter;
	private Button leftBtn,rightBtn;
	private TextView title;
	//private ImageView add_img;
	
	//private String [] name={"张三","李四","王五","路人1",
	//		"路人2","路人3","路人4","路人5","路人6","路人7"};
	//private int[] add_imgArr = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
	//		R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
	//		R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};	
	
	//private int[] addImg = {R.drawable.add,R.drawable.clear,R.drawable.msg};

	private int length ;     //通讯录列表的数目
    private int fd_lenth = Item08View.study_list.size(); //学友列表的数目
    
	private int[] num;//类型编号
	private static final int ADD_PARAM = 0;     //添加
	private static final int ADDED_PARAM = 1;   //已添加
	private static final int INVITE_PARAM = 2;  //邀请
	
	private String[] invite_num ;//邀请标志的号码
	
	private SharePreferenceUtil mSpUtil;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.chat_add_friend);
		mSpUtil = HSApplication.getInstance().getSpUtil();
		initView();
		initData();
		
	}
	
    public void initData(){
    	L.Debug("Item08View.contacts_length",""+Item08View.contacts_length);
    	invite_num = new String[Item08View.ie_length];
    	invite_num = getIntent().getStringArrayExtra("invite_num");

    	length = Item08View.contacts.size();
    	L.Debug("length", ""+length);
		num = new int[length];
		//String invite_tmp = new String(invite_num);
		//String[] invite_str = invite_tmp.split("\\|");
		for(int i=0;i<length;i++){
			num[i]=ADD_PARAM;  //初始化设置都为添加的标志
			String tel_num = Item08View.contacts.get(i).getNum();
			//遍历判断是否为邀请标志的号码
			for(int j=0;j<invite_num.length;j++){				
				if(tel_num.equals(invite_num[j])){
					num[i]=INVITE_PARAM;
				   	L.Debug("invite_num["+j+"]", invite_num[j]);
				}				
			}
			
			//遍历判断是否为已添加标志的号码
			for(int k=0;k<fd_lenth&&num[i]!=INVITE_PARAM;k++){
				if(tel_num.equals(Item08View.study_list.get(k).getTelNum())){
					num[i] = ADDED_PARAM;
				}
			}
			//adapter.notifyDataSetChanged();

		}

    }
	public void initView(){
		leftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		title=(TextView)findViewById(R.id.layout_title_middle_text);
		rightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		//add_img = (ImageView)findViewById(R.id.add_img);
		
		title.setText("添加学友");
		leftBtn.setText("返回");
		rightBtn.setText("邀请达人榜");		
		leftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				if(Item08View.adapter!=null)
//				Item08View.adapter.notifyDataSetChanged();
				finish();
			}
		});
		
		rightBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.toast(context, "此功能暂不支持");
			}
		});
		
	//		add_img.setOnClickListener(new OnClickListener() {
	//			@Override
	//			public void onClick(View v) {
	//				Util.toast(context, "onClick add_img");
	//				Intent intent = new Intent(AddFriendActivity.this,AddTelActivity.class);
	//				startActivity(intent);
	//			}
	//		});
		add_listview = (ListView)findViewById(R.id.item_add_listview);
		adapter = new AddListViewAdapter(this);
		add_listview.setAdapter(adapter);
		add_listview.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {

			}
			});
		
	}
	/**
	 * 加好友列表适配器
	 * @author Leone
	 *
	 */
	private String curNum = "10086";
	@SuppressLint("ResourceAsColor")
	class AddListViewAdapter extends BaseAdapter{
		private LayoutInflater inflater;
		public AddListViewAdapter(Context mContext){
			inflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			return length;//name.length
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			Holder holder = null;

			if(convertView == null){
				convertView = inflater.inflate(R.layout.chat_add_listview, null);
				holder = new Holder();
            	//holder.img = (ImageView)convertView.findViewById(R.id.add_head_img);
				holder.textTitle = (TextView)convertView.findViewById(R.id.add_name);
				holder.add = (Button)convertView.findViewById(R.id.add_img2);				
				//holder.num = (TextView)convertView.findViewById(R.id.add_num);
            	convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
			
			//holder.img.setBackgroundResource(R.drawable.ic_launcher);
			holder.textTitle.setText(Item08View.contacts.get(position).getName());
			
			L.Debug("contacts"+position+"name", Item08View.contacts.get(position).getName());
			holder.textTitle.setBackgroundColor(0xf2f2f2);
			int type = num[position];
			switch(type){
			case ADD_PARAM:
				holder.add.setText(" ");
				holder.add.setBackgroundResource(R.drawable.add);
				break;
			case ADDED_PARAM:
				holder.add.setText("已添加");
				holder.add.setBackgroundResource(R.drawable.comment_btn_normal);
				break;
			case INVITE_PARAM:
				holder.add.setText("邀请");
				holder.add.setBackgroundResource(R.drawable.comment_btn_normal);
				break;
			
				
			}
			
			final String typeNum = String.valueOf(num[position]);
			//holder.num.setText(typeNum);
			 
			
			holder.add.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//Util.toast(context, "onClick add");
					switch(Integer.parseInt(typeNum)){
					case 0:
						//Util.toast(context, "此功能暂不支持");
						curNum = Item08View.contacts.get(position).getNum();
						AddFdRequest();
						break;
					case 1:
						Util.toast(context, "已是好友");
						break;
					case 2:
						//Util.toast(context, "此功能暂不支持");
						curNum = Item08View.contacts.get(position).getNum();
		                Uri uri = Uri.parse("smsto:"+curNum); 
		                Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
		                it.putExtra("sms_body", "移动小炮已经瞄准你啦！！！"); 
		                startActivity(it); 
						break;
					}

				}
			});
			return convertView;
		}
		
		class Holder{
        	//ImageView img;
        	TextView textTitle;
        	Button add;
        	//TextView num;
        	ImageView typeImg;
        }
		
		
	}	
    private static AsyncHttpClient client = new AsyncHttpClient();
	public void AddFdRequest(){
    	RequestParams params = new RequestParams();
        String fromMobile =HSApplication.sp.getString("mobile","");
        String toMobile = curNum;
		params.put("type", "1");
		params.put("fromMobile", fromMobile); //mSpUtil.getMobile()
		params.put("toMobile", toMobile);             //curNum
		client.post(NetContact.LOAD_FRIEND_REQUEST_URL,
				params, new JsonHttpResponseHandler() {
	
					
					@Override
					public void onSuccess(JSONObject response) {
						L.Debug("AddRequest response", response.toString());
						if(response.toString().equals("对方没有推送标记")){
							L.showShort(context, "华商网没有此联系人");
						}
						//						else{
						//							JSONArray jsonArray;
						//							try {
						//								jsonArray = response.getJSONArray("response");
						//								L.Debug("请求加好友  response", jsonArray.toString());
						//
						//								//			                    Intent intent = new Intent(context,AddFriendActivity.class);
						//								//			                    intent.putExtra("invite_num", invite_num);
						//								//			                    context.startActivity(intent);  
						//							} catch (JSONException e) {
						//								e.printStackTrace();
						//								L.showShort(context, "华商网没有此联系人");
						//								L.Debug("请求加好友 JSONException", e.getMessage());
						//							}							
						//						}

								
					}

				});		
	}
}
